<template>
<body id="poster">

  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/Fish.png" />
      </div>
      <!-- 登录表单 -->
      <!-- ref这个引用对象就是el-form的实例 -->
    
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_from"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont el-icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont el-icon-lock"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    
    </div>
  </div>
  
  </body>
</template>

<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.15.3/lib/index.js"></script>
<script>
import Cookies from 'js-cookie'
export default {
  data() {
    
    return {
     
      loginForm: {
        username: "",
        password: "",
      },
      //表单验证规则
      loginFormRules: {
        //验证用户名
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        //验证密码
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 10,
            message: "长度在 6 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 点击重置表单
    resetLoginForm() {
      //console.log(this);
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        }
        //将表单用户名暂时保存在username中
        Cookies.set('username',this.loginForm.username);
        const { data: res } = await this.$http.post("user/login", this.loginForm);
        //console.log(res);
        if (res.code != 200) return this.$message.error("登录失败！"); //code为200表示登录成功，2003表示登录密码或用户名错误，2004用户账户已禁用
        this.$message.success("登录成功！");
        // 判断是否登录成功，成功后才可访问home组件
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>

body{
  margin:0px;
  padding:0;
}

.login_container {
   height: 100%;
   background:url("../assets/bpp.png");
    width: 100%;
    position:fixed;
 
    background-size:100% 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background:rgba(255,255,255,0.5);
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_from {
   position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;

}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
